package com.cskaoyan.service.wx.sl;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bo.wx.sl.WxCollectAddordeleteBo;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.vo.wx.sl.WxCollectListBean;
import com.cskaoyan.vo.wx.sl.WxCollectListBeanVoo;
import com.cskaoyan.vo.wx.sl.WxCollectListVo;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCollectsServiceImpl implements WxCollectsService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    //显示商品收藏列表
    @Override
    public WxCollectListVo collectList(byte type, Integer page, Integer size) {
        //分页排序
        PageHelper.startPage(page, size);
        //先筛选type=0的
        User usere = (User) SecurityUtils.getSubject().getPrincipal();
        Integer usereId = usere.getId();
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(usereId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        List<WxCollectListBeanVoo> collectbeans = new ArrayList<>();
        //然后遍历
        for (Collect collect : collects) {
            //取出deleted
            Boolean deleted = collect.getDeleted();
            if (deleted.equals(false)){
                //取出valueId=goodsId 然后去查goods表
                Integer valueId = collect.getValueId();
                Integer id = collect.getId();
                WxCollectListBeanVoo voo = new WxCollectListBeanVoo();
                WxCollectListBean wxCollectListBean = goodsMapper.queryCollectList(valueId);
                voo.setType(type);//收藏表的信息
                voo.setValueId(valueId);//goods表的信息
                voo.setId(id);//收藏表的信息
                //把goods的值修改到voo内
                String brief = wxCollectListBean.getBrief();
                String pic_url = wxCollectListBean.getPic_url();
                String name = wxCollectListBean.getName();
                double retail_price = wxCollectListBean.getRetail_price();
                voo.setBrief(brief);
                voo.setPicUrl(pic_url);
                voo.setName(name);
                voo.setRetailPrice(retail_price);
                collectbeans.add(voo);
            }
        }
        WxCollectListVo listVo = new WxCollectListVo(page, collectbeans);
        return listVo;
    }
    //添加或者删除收藏
    @Override
    public int collectAddordelete(WxCollectAddordeleteBo wxCollectAddordeleteBo) {
        byte type = wxCollectAddordeleteBo.getType();
        Integer valueId = wxCollectAddordeleteBo.getValueId();
        //先查询
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andTypeEqualTo(type).andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        int i = 0;
        User usere = (User) SecurityUtils.getSubject().getPrincipal();
        Integer usereId = usere.getId();
        if (collects.size() == 0){
            //插入
            Collect coll = new Collect();
            coll.setValueId(valueId);
            coll.setType(type);
            coll.setAddTime(new Date());
            coll.setUpdateTime(new Date());
            //设定一个假的userid
            coll.setUserId(usereId);
            coll.setDeleted(false);
            //插入数据库
            int num = collectMapper.insertSelective(coll);
            i = 200;
            return i;
        }
        for (Collect collect : collects) {
            Boolean deleted = collect.getDeleted();
            Integer id = collect.getId();
            //取消收藏，把deleted改为true
            Collect update = new Collect();
            update.setId(id);
            update.setDeleted(true);
            int num = collectMapper.updateByPrimaryKeySelective(update);
            i = 2;
            return i;
        }
        return i;
    }
}
